import { Component, OnInit } from '@angular/core';
import { MessageService } from 'src/app/core/service/message.service';
import { ApiService } from 'src/app/core/api';
import { NzMessageService } from 'ng-zorro-antd';
import { RegionNumber } from 'src/app/model';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { I18nService } from 'src/app/core/i18n/i18n.service';
import { Router } from '@angular/router';

@Component({
    templateUrl: './index.html',
    styleUrls: ['./index.scss']
})
export class XGMMComponent implements OnInit {
    private _current = 0;

    nzSelectedIndex = 0;
    regionNumbers: RegionNumber[] = [];

    resetPwsForm: FormGroup;


    authCodeLable = '发送验证码';
    authCodeLableType = false;
    ngOnInit() {
        this.message.setBreadcrumb({
            breadcrumbs: [
                { name: '我要检测' }
            ]
        });
        this.resetPwsForm = this.fb.group({
            oldLoginPw: [null, [Validators.required]],
            loginPw: [null, [Validators.required]],
            reLoginPw: [null, [Validators.required, this.confirmationValidator]],
        });
    }

    submit(data) {
        this.api.post('修改密码(通过老密码)', data).then((res: any) => {
            this.msg.success('修改成功');
            this.router.navigateByUrl('/member/center/home/login');
        }).catch((res) => {
            this.msg.error(res.message);
        });
    }

    private confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
        if (!control.value) {
            return { required: true };
        } else if (control.value !== this.resetPwsForm.controls.newPwd.value) {
            return { confirm: true, error: true };
        }
    }
    
    constructor(
        private fb: FormBuilder,
        private message: MessageService,
        private msg: NzMessageService,
        private router: Router,
        private api: ApiService
    ) { }
}